package edu.uj.petri.controller;

import edu.uj.petri.commands.AddPlaceCommand;
import edu.uj.petri.commands.Command;
import edu.uj.petri.document.Document;
import edu.uj.petri.view.DiagramElement;
import java.awt.Point;
import java.awt.geom.Point2D;

public class NewPlaceEventHandler implements EventHandler {
   
    public NewPlaceEventHandler()
    {
        Document.getActiveDocument().getView().selectNone();
        if (NewInhibitorArcEventHandler.unfinished != null) 
        {
            Document.getActiveDocument().getView().removeElement(NewInhibitorArcEventHandler.unfinished);
            Document.getActiveDocument().getView().draw();
            NewInhibitorArcEventHandler.unfinished = null;
            
        } else if (NewNormalArcEventHandler.unfinished != null)  
        {
            Document.getActiveDocument().getView().removeElement(NewNormalArcEventHandler.unfinished);
            Document.getActiveDocument().getView().draw();
            NewNormalArcEventHandler.unfinished = null;
        }
    }

    public void click(double x, double y) {
        
        Command c = new AddPlaceCommand( Document.getActiveDocument(), new Point2D.Double( x, y));
        Document.getActiveDocument().execute( c );        
    }

    public void doubleClick(double x, double y) {
    }

    public void mouseMove(double x1, double y1, int button) {
    }

    public void mouseButtonDown(double x, double y) {
    }

    public void mouseButtonUp(double x, double y) {
    }

    public void rightClick(double x, double y) {
        
        DiagramElement selected = Document.getActiveDocument().getView().getClickedElement( new Point2D.Double( x, y ), false);
        if (selected != null) {
            
            Point point = Document.getActiveDocument().getView().getCanvas().convertCanvasToScreen(x,y);
            selected.getContextMenu().show( Document.getActiveDocument().getView().getCanvas().getComponent(), (int)point.getX(), (int)point.getY());
            Document.getActiveDocument().getView().draw();  
        }
    }

    /**
     * handles Escape and Delete
     */
    public void keyPressed(int keyCode) {
    }

    public void mouseDragged(double x, double y) {
    }


}
